<?php
namespace haibao\cms\data;

class Manager extends \haibao\cms\data\BaseMysql{
	
	public function __construct(){
		parent::__construct('\haibao\cms\model\data\BDKeyword');
	}
	
	public function getBdChannels(){
		$data = array();
		$sql = 'select distinct(Channel) from BDKeyword';
		$result = $this->query($sql);
		while ($result->fetch_assoc()){
			array_push($data,$result->getData('Channel'));
		}
		
		return $data;
	}
	
	public function searchBdChannelSimilar($name,$pager,$isKeyword = false){
		$data = array();
		$columnName = '';
		if ($isKeyword){
			$columnName = 'Keyword';
		} else {
			$columnName = 'Channel';
		}
		
		$sql = 'select count(distinct('.$columnName.')) as num from BDKeyword where '.$columnName.' like "%'.$name.'%"';
		$result = $this->query($sql);
		if ($result->fetch_assoc()){
			$pager->setResultCount($result->getData('num')); 
		}
		
		if ($pager->getResultCount() > 0){
			$sql = 'select distinct('.$columnName.') from BDKeyword where '.$columnName.' like "%'.$name.'%" limit '.(($pager->getCurrentPage()-1)*$pager->getPageSize()).','.$pager->getPageSize();
			$result = $this->query($sql);
			while ($row = $result->fetch_assoc()){
				$model = new \haibao\cms\model\data\BDKeyword();
				$model->loadArray($row);
				array_push($data,$model);
			}
		}
		
		return $data;
	}
}